package com.haidechizi.longpolling.utils;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;

/**
 * @author wangdejun
 * @create 2021/2/24 13:18
 */
@Slf4j
public class ResponseUtils {

    private ResponseUtils() {

    }

    public static final void generatorResponse(HttpServletResponse response, Object data) {
        generatorResponseString(response, JSON.toJSONString(data));
    }

    public static final void generatorResponseString(HttpServletResponse response, String data) {
        try {
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control", "no-cache,no-store");
            response.setContentType("application/json;charset=UTF-8");//json格式
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().println(data);
        } catch (Exception e) {
            log.error("返回结果出现异常", e);
        }
    }
}
